package com.ehotting.edsta.mqbd.monitor.task.job;

import com.ehotting.eaf.core.DateHelper;
import com.ehotting.eaf.redis.service.RedisService;
import com.ehotting.mqbd.api.MQDBConstants;
import com.ehotting.mqbd.core.monitor.ActiveManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Date;

public class ServerActiveTask {

    private Logger logger = LoggerFactory.getLogger(ServerActiveTask.class);

    private ActiveManager activeManager;

    private RedisService redisService;

    @Scheduled(initialDelay=10000, fixedRate=10000)
    public void invoke(){
        logger.info("Start ServerActiveTask");

        boolean lock = redisService.checkLock(MQDBConstants.REDIS_LOCK_KEY_MONITOR,DateHelper.toString(new Date(),DateHelper.STD_FORMAT),1000L*30);
        if(lock == false){
            logger.info("ServerActive is locked,return.");
            return;
        }
        logger.info("Start active action");

        long startTime = System.currentTimeMillis();
        activeManager.invoke();
        logger.info("Finish ServerActiveTask,cost time:{}",(System.currentTimeMillis()-startTime));
    }


    public void setActiveManager(ActiveManager activeManager) {
        this.activeManager = activeManager;
    }

    public void setRedisService(RedisService redisService) {
        this.redisService = redisService;
    }
}
